package com.thinkgem.jeesite.modules.app.api;


import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import com.thinkgem.jeesite.modules.app.entity.PhoneMessage;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.core.MediaType;


/**
 * Created by zhangpeng on 16/9/10.
 */
public class SendMessage {

//        public  int send(){
//            SendMessage api = new SendMessage();
//            String httpResponse =  api.testSend();
//            try {
//                JSONObject jsonObj = new JSONObject( httpResponse );
//                int error_code = jsonObj.getInt("error");
//                String error_msg = jsonObj.getString("msg");
//                if(error_code==0){
//                    System.out.println("Send message success.");
//                }else{
//                    System.out.println("Send message failed,code is "+error_code+",msg is "+error_msg);
//                }
//                return error_code;
//            } catch (JSONException ex) {
//                Logger.getLogger(SendMessage.class.getName()).log(Level.SEVERE, null, ex);
////                return -1;
//            }
//
////            httpResponse =  api.testStatus();
////            try {
////                JSONObject jsonObj = new JSONObject( httpResponse );
////                int error_code = jsonObj.getInt("error");
////                if( error_code == 0 ){
////                    int deposit = jsonObj.getInt("deposit");
////                    System.out.println("Fetch deposit success :"+deposit);
////                }else{
////                    String error_msg = jsonObj.getString("msg");
////                    System.out.println("Fetch deposit failed,code is "+error_code+",msg is "+error_msg);
////                }
////            } catch (JSONException ex) {
////                Logger.getLogger(SendMessage.class.getName()).log(Level.SEVERE, null, ex);
////            }
//
//            return -1;
//
//        }

        public String testSend(PhoneMessage phoneMessage){
            // just replace key here
            Client client = Client.create();
            client.addFilter(new HTTPBasicAuthFilter(
                    "api","key-93e5d55d12857d8885680f35d873cc8f"));
            WebResource webResource = client.resource(
                    "http://sms-api.luosimao.com/v1/send.json");
            MultivaluedMapImpl formData = new MultivaluedMapImpl();
            formData.add("mobile", phoneMessage.getMobile());
            formData.add("message", "您在社区服务平台请求的验证码是：" + phoneMessage.getCode() +"【众联恒业】");
            ClientResponse response =  webResource.type( MediaType.APPLICATION_FORM_URLENCODED ).
                    post(ClientResponse.class, formData);
            String textEntity = response.getEntity(String.class);
            int status = response.getStatus();
            System.out.print(textEntity);
            System.out.print(status);
            return textEntity;
        }

        private String testStatus(){
            Client client = Client.create();
            client.addFilter(new HTTPBasicAuthFilter(
                    "api","key-c0348475f413faf2e8eb03f9ba7f0507"));
            WebResource webResource = client.resource( "http://sms-api.luosimao.com/v1/status.json" );
            MultivaluedMapImpl formData = new MultivaluedMapImpl();
            ClientResponse response =  webResource.get( ClientResponse.class );
            String textEntity = response.getEntity(String.class);
            int status = response.getStatus();
            System.out.print(status);
            System.out.print(textEntity);
            return textEntity;
        }
    }

